﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;
using System.Web.UI.HtmlControls;
namespace TMDT_Nhom1A.UserControl
{
    public partial class DanhSachNhomSanPham : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {


            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            rplistkeycatalogue.DataSource = ParentCatalogue_BUS.ListKeyCatalogue();
            rplistkeycatalogue.DataBind();
            if (Request.QueryString["Name"] == null)
            {
                HtmlGenericControl dt = (HtmlGenericControl)rplistkeycatalogue.Items[0].FindControl("dt");
                dt.Attributes.Add("class", "accordactive");
                HtmlGenericControl dd = (HtmlGenericControl)rplistkeycatalogue.Items[0].FindControl("dd");
                dd.Attributes.Add("style", "display:block;");
            }
            else
            {
                for (int i = 0; i < rplistkeycatalogue.Items.Count; i++)
                {
                    HtmlGenericControl dd = (HtmlGenericControl)rplistkeycatalogue.Items[i].FindControl("dd");
                    dd.Attributes.Add("style", "display:none;");
                    HtmlAnchor ParentCatalogue_Name = (HtmlAnchor)rplistkeycatalogue.Items[i].FindControl("ParentCatalogue_Name");
                    if (ParentCatalogue_Name.InnerText.Contains(Request.QueryString["Name"]))
                    {
                        HtmlGenericControl dt = (HtmlGenericControl)rplistkeycatalogue.Items[i].FindControl("dt");
                        dt.Attributes.Add("class", "accordactive");
                        dd.Attributes.Add("style", "display:block;");
                        break;
                    }
                    else
                    {
                        Repeater rplistcatalogue = (Repeater)rplistkeycatalogue.Items[i].FindControl("rplistcatalogue");
                        for (int j = 0; j < rplistcatalogue.Items.Count; j++)
                        {
                            dd.Attributes.Add("style", "display:none;");
                            HtmlAnchor ChildCatalogue_Name = (HtmlAnchor)rplistcatalogue.Items[j].FindControl("ChildCatalogue_Name");
                            if (ChildCatalogue_Name.InnerText.Contains(Request.QueryString["Name"]))
                            {
                                HtmlGenericControl dt = (HtmlGenericControl)rplistkeycatalogue.Items[i].FindControl("dt");
                                dt.Attributes.Add("class", "accordactive");
                                dd.Attributes.Add("style", "display:block;");
                                break;
                            }
                        }
                    }
                }
            }
        }

        protected void rplistkeycatalogue_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label parentcataloguekey = (Label)e.Item.FindControl("keycatalogueid");
            Repeater rplistcatalogue = (Repeater)e.Item.FindControl("rplistcatalogue");
            List<CHILD_CATALOGUE> listchildcatalogue = ChildCatalogue_BUS.ListCatalogue(int.Parse(parentcataloguekey.Text));
            if (listchildcatalogue.Count != 1 && listchildcatalogue[0].ChildCatalogue_Name != "")
            {
                rplistcatalogue.DataSource = listchildcatalogue;
                rplistcatalogue.DataBind();
                HtmlImage imgaccord = (HtmlImage)e.Item.FindControl("imgaccord");
                imgaccord.Visible = true;
            }
        }



        protected void btsearch_Click(object sender, EventArgs e)
        {

            string Url = Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port + "/San_Pham/TimKiemNangCao.aspx?keyword=" + tbkeyword.Text; 
            Response.Redirect(Url);
        }


    }
}